package com.itheima.controller;

import com.itheima.bean.User;
import com.itheima.service.UserService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;


        /**
     * 处理登录的请求
     */
    @PostMapping("/login")
    protected boolean login(@RequestBody User user, HttpSession session){
        User existUser=userService.login(user);
        //3 判断如果user!=null,说明登录成功，将user保存到session
        if(existUser!=null){
            session.setAttribute("user",existUser);
        }
        return existUser!=null;
    }
}
